/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package server;

import java.io.*;
import java.net.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author sun
 */
public class SE {

    LinkedHashMap<String, MT> sl;
    ServerSocket ss;
    Socket a;
    LinkedList<MT> ml;
    public static final char LOGIN = 0xF0;
    public static final char LOGOUT = 0xF1;
    public static final char COMMAND = 0xFD;
    public static final char CHATTO = 0xF2;
    public static final char ADDFRIENDS = 0xF3;
    public static final char DELETEFRIENDS = 0xF4;
    public static final char REGIST = 0xF5;
    public static final char ISONLINE = 0xF6;
    public static final char GETFRIENDS = 0xF7;
    public static final char CHAT = 0xF8;
    FriendData fd;
    DataSource ds;

    public String proc(String a, MT m) {
        String[] var = a.substring(1).split(" ");
        switch (a.charAt(0)) {
            case SE.ADDFRIENDS:

                break;
            case SE.REGIST:
                String name = var[0];
                String pass = var[1];
                System.out.println("reg");
                if (((SqlDataSourse) ds).hasUser(name)) {
                    String ms = SE.REGIST + "b";
                    m.send(ms);
                } else {
                    if (ds.addUser(name, pass)) {
                        String ms = SE.REGIST + "y";
                        m.send(ms);
                    } else {
                        String ms = SE.REGIST + "n";
                        m.send(ms);
                    }
                }
                break;
            case SE.GETFRIENDS:
                
                break;
            case SE.DELETEFRIENDS:
                break;
            case SE.LOGIN:
                String name1 = var[0];
                String pass1 = var[1];
                if (ds.verifyUser(name1, pass1)) {
                    sl.put(name1, m);
                    ml.remove(m);
                    m.send(SE.REGIST + "y");
                } else {
                    m.send(SE.REGIST + "N");
                }
                break;
            case SE.LOGOUT:
                sl.remove(m.getmName());
                break;
            case SE.CHATTO:
                String msg = var[var.length-1];
                for(int i=0;i<(var.length-1);i++){
                    MT t = sl.get(var[i]);
                    t.send(msg);
                }
                break;
            case SE.CHAT:

                break;

        }
        return null;
    }

    public SE() {
        sl = new LinkedHashMap<String, MT>();
        ds = new SqlDataSourse();
        ml = new LinkedList<MT>();

        try {
            ss = new ServerSocket(12345);
        } catch (IOException ex) {
            Logger.getLogger(SE.class.getName()).log(Level.SEVERE, null, ex);
        }
        while (true) {
            try {
                a = ss.accept();
//                BufferedReader in = new BufferedReader(new InputStreamReader(a.getInputStream()));
//                String n = in.readLine();
//                String[] var = n.substring(1).split(" ");
                MT m = new MT(a, this);
                m.start();
                System.out.println(m);
                ml.add(m);
            } catch (IOException ex) {
                Logger.getLogger(SE.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    
    public static void main(String[] args) {
        SE se = new SE();
    }
}
